package com.kly.cashmall.utils.function;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import androidx.annotation.NonNull;

/**
 * @author zxp
 * @since 27/10/21
 */
public class PackageInfoUtil {
    public PackageInfoUtil() {
    }

    public static String getVersionName(@NonNull Context context) {
        String versionName = "";

        try {
            PackageInfo packageInfo = context.getApplicationContext().getPackageManager().getPackageInfo(context.getApplicationContext().getPackageName(), 0);
            versionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException var3) {
            var3.printStackTrace();
        }

        return versionName;
    }

    public static int getVersionCode(@NonNull Context context) {
        int versionCode = 0;

        try {
            PackageInfo packageInfo = context.getApplicationContext().getPackageManager().getPackageInfo(context.getApplicationContext().getPackageName(), 0);
            versionCode = packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException var3) {
            var3.printStackTrace();
        }

        return versionCode;
    }

    public static int getTargetSdkVersion(@NonNull Context context) {
        return context.getApplicationContext().getApplicationInfo().targetSdkVersion;
    }

    public static String getApplicationPackageName(@NonNull Context context) {
        return context.getApplicationInfo().packageName;
    }
}
